package com.kedacom.freedomLabel.view.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import com.kedacom.freedomLabel.R;
import com.kedacom.freedomLabel.databinding.ProductSelectItemLayoutBinding;
import com.kedacom.freedomLabel.model.bean.IMainSelectItem;
import com.kedacom.freedomLabel.utils.validator.EditTextValidator;
import com.kedacom.freedomLabel.utils.validator.EmptyValidation;
import com.kedacom.freedomLabel.utils.validator.ValidationModel;
import com.lzscale.scalelib.misclib.Misc;

import androidx.annotation.Nullable;

/**
 * @author licheng
 * @since 2020/3/14 15:21
 */
public class ProductSelectItemView extends RelativeLayout {

    EditTextValidator mEditTextValidator;
    Context mContext;
    ProductSelectItemLayoutBinding mBinding;
    View.OnClickListener mOnClickListener;
    DeleteSelectItemCallback mDeleteSelectItemCallback;

    Misc mMisc = Misc.newInstance();

    protected ProductSelectItemView(Context context) {
        this(context, null);
    }

    protected ProductSelectItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected ProductSelectItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mEditTextValidator = new EditTextValidator(context);
        LayoutInflater inflater = LayoutInflater.from(context);
        mBinding = DataBindingUtil.inflate(inflater, R.layout.product_select_item_layout, this, false);
        addView(mBinding.getRoot());
        mBinding.productItemClear.setOnClickListener(view -> {
            mMisc.beep();
            setResult(null, null);
            if (mDeleteSelectItemCallback != null) {
                mDeleteSelectItemCallback.deleteSelectItem(getItemCategory());
            }
        });
        mBinding.productItemValue.setOnClickListener(view -> {
            mMisc.beep();
            if (mOnClickListener != null) {
                mOnClickListener.onClick(ProductSelectItemView.this);
            }
        });
    }

    protected void setDeleteSelectItemCallback(DeleteSelectItemCallback deleteSelectItemCallback) {
        mDeleteSelectItemCallback = deleteSelectItemCallback;
    }

    protected interface DeleteSelectItemCallback {
        void deleteSelectItem(String category);
    }

    protected boolean isReadySubmit() {
        return mEditTextValidator.validate();
    }

    protected void setItemInformation(String itemName, String category, Boolean isRequired) {
        mEditTextValidator.clear();
        mBinding.productItemName.setText(itemName);
        setTag(R.integer.tag_product_item_category, category);
        setTag(R.integer.tag_product_item_required, isRequired);
        if (isRequired) {
            mEditTextValidator.add(new ValidationModel(mBinding.productItemValue,
                    new EmptyValidation("请选择" + itemName)));
        }
    }

    protected void setItemViewClickListener(View.OnClickListener clickListener) {
        this.mOnClickListener = clickListener;
    }

    protected void setResult(String itemValue, IMainSelectItem selectItem) {
        mBinding.productItemValue.setText(itemValue);
        setTag(R.integer.tag_product_item_result, selectItem);
    }

    public String getItemCategory() {
        Object tag = getTag(R.integer.tag_product_item_category);
        return tag == null ? null : tag.toString();
    }

    protected IMainSelectItem getResult() {
        Object tag = getTag(R.integer.tag_product_item_result);
        if (tag instanceof IMainSelectItem) return (IMainSelectItem) tag;
        return null;
    }
}